﻿using System;

public abstract class State<T>
{
    public abstract void Execute(T miner);
    public abstract void Enter(T miner, object args = null);
    public abstract void Exit(T miner);
}

public class StateMachine<T>
{
    private T owner;
    private State<T> currentState;
    private State<T> globalState;
    private State<T> previousState;

    public StateMachine(T _owner)
    {
        owner = _owner;
        currentState = null;
        globalState = null;
        previousState = null;
    }

    public void Update()
    {
        if (globalState!=null)
        {
            globalState.Execute(owner);
        }
        if (currentState != null)
        {
            currentState.Execute(owner);
        }
    }

    public void ChangeState(State<T> newState, object args = null)
    {
        if (newState==null)
        {
            Console.WriteLine("[StateManager]:状态切换失败");
            return;
        }
        previousState = currentState;
        currentState.Exit(owner);
        currentState = newState;
        currentState.Enter(owner, args);
    }

    //属性
    public void RevertToPreviosState()
    {
        ChangeState(previousState);
    }
    public State<T> CurrentState
    {
        get
        {
            return currentState;
        }
        set 
        {
            currentState = value;
        }
    }
    public State<T> GlobalState
    {
        get
        {
            return globalState;
        }
        set 
        {
            globalState = value;
        }
    }
    public State<T> PreviousState
    {
        get
        {
            return previousState;
        }
        set 
        {
            previousState = value;
        }
    }
}